/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using EnvDTE;
using EnvDTE80;

using Microsoft.Practices.RecipeFramework;
using Microsoft.Practices.RecipeFramework.Extensions.CommonHelpers;
using Microsoft.Practices.RecipeFramework.Library;

namespace DslEditorPowerToy.Recipes.Actions
{
    /// <summary>
    /// Writes a trace message to status bar and optionally output window pane
    /// </summary>
    public class WriteTraceAction : ActionBase
    {
        private string _message = string.Empty;
        private OutputWindowPane _pane = OutputWindowPane.General;

        #region Constructors
        public WriteTraceAction()
            : base() 
        { 
        }
        #endregion

        #region Public Properties
        [Input(Required=true)]
        public string Message
        {
            get
            {
                return this._message;
            }
            set
            {
                this._message = value;
            }
        }
        [Input(Required=false)]
        public OutputWindowPane Pane
        {
            get
            {
                return this._pane;
            }
            set
            {
                this._pane = value;
            }
        }        
        #endregion

        #region IAction Members

        public override void Execute()
        {
            base.Execute();
            ValidateParameters();

            //Expand message
            string messageText = (string)this.Evaluate(this._message);
            string contextualMessage = string.Format("{0}: {1}", this.Package.Caption, messageText);

            //Trace message
            Trace.TraceInformation(contextualMessage);

            //Write message to Status bar
            this.DTE.StatusBar.Text = contextualMessage;

            //Write message to Output Window
            if (OutputWindowPane.None != this._pane)
            {
                //Only message text if writing to Package pane (no-context required)
                string outputMessage = contextualMessage;
                if (OutputWindowPane.Package == this._pane)
                    outputMessage = messageText;

                DteHelperEx.ShowMessageInOutputWindow(this.DTE, outputMessage, GetPaneName());
            }
        }
        public override void Undo()
        {
            //Do undo action, or explain why you can't or should not
        }

        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates parameters before execution
        /// </summary>
        protected override void ValidateParameters()
        {
            if (null == this._message)
                throw new ArgumentNullException("Message");
            if (0 == this._message.Length)
                throw new ArgumentOutOfRangeException("Message");
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Returns the name of the current pane
        /// </summary>
        /// <returns></returns>
        private string GetPaneName()
        {
            if (OutputWindowPane.Package == this._pane)
                return this.Package.Caption;
            else
                return this._pane.ToString();
        }
        #endregion
    }

    public enum OutputWindowPane
    {
        /// <summary>
        /// no output pane
        /// </summary>
        None,
        /// <summary>
        /// General pane
        /// </summary>
        General,
        /// <summary>
        /// Build pane
        /// </summary>
        Build,
        /// <summary>
        /// This recipe guidance package
        /// </summary>
        Package
    }
}
